﻿using FHJFPro.Base;
using FHJFPro.Domain;
using FHJFPro.UserInfos.Dto;
using FHJFPro.UserInfos.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FHJFPro.UserInfos
{
    public class UserInfoService : FHJFProAppService, IUserInfoService
    {
        private readonly IUserInfoRepository _userInfoRepository;
        private readonly UserInfoManager _userInfoManager;
        public UserInfoService(IUserInfoRepository userInfoRepository, UserInfoManager userInfoManager)
        {
            _userInfoRepository = userInfoRepository;
            _userInfoManager = userInfoManager;
        }
        public async Task<ResponseResult<UserInfoDto>> CreateAsync(CreateUserInfoDto input)
        {
            var result = new ResponseResult<UserInfoDto>();
            var userinfo = await _userInfoManager.CreateAsync(input.UserName, input.Password, input.Gender, input.Description);
            await _userInfoRepository.InsertAsync(userinfo, true);
            result.IsSuccess(ObjectMapper.Map<UserInfo, UserInfoDto>(userinfo));
            return await Task.FromResult(result);
        }

        public async Task<ResponseResult<IEnumerable<UserInfoDto>>> CreateManyAsync(IEnumerable<CreateUserInfoDto> input)
        {
            var result = new ResponseResult<IEnumerable<UserInfoDto>>();
            var userInfos = new List<UserInfo>();
            foreach (var item in input)
            {
                var userInfo = await _userInfoManager.CreateAsync(item.UserName, item.Password, item.Gender, item.Description);
                userInfos.Add(userInfo);
            }
            await _userInfoRepository.InsertManyAsync(userInfos);
            result.IsSuccess(ObjectMapper.Map<IEnumerable<UserInfo>, IEnumerable<UserInfoDto>>(userInfos));
            return await Task.FromResult(result);
        }

        public async Task<ResponseResult<UserInfoDto>> FindByNameAsync(string name)
        {
            var result = new ResponseResult<UserInfoDto>();
            var userInfo = await _userInfoRepository.FindByNameAsync(name);
            result.IsSuccess(ObjectMapper.Map<UserInfo, UserInfoDto>(userInfo));
            return await Task.FromResult(result);
        }

        public async Task<ResponseResult<IEnumerable<UserInfoDto>>> GetAllAsync()
        {
            var result = new ResponseResult<IEnumerable<UserInfoDto>>();
            var userInfos = await _userInfoRepository.GetListAsync();
            result.IsSuccess(ObjectMapper.Map<IEnumerable<UserInfo>, IEnumerable<UserInfoDto>>(userInfos));
            return await Task.FromResult(result);
        }

        public async Task<ResponseResult<UserInfoDto>> GetAsync(Guid id)
        {
            var result = new ResponseResult<UserInfoDto>();
            var userInfo = await _userInfoRepository.GetAsync(id);
            result.IsSuccess(ObjectMapper.Map<UserInfo, UserInfoDto>(userInfo));
            return await Task.FromResult(result);
        }

        public async Task<ResponseResult<List<UserInfoDto>>> GetPageListAsync(GetUserInfoListDto input)
        {

            var result = new ResponseResult<List<UserInfoDto>>();
            if (input.Sorting.IsNullOrWhiteSpace())
            {
                input.Sorting = nameof(UserInfo.LastModificationTime) + " desc";
            }
            var data = await _userInfoRepository.GetListAsync(input.SkipCount, input.MaxResultCount, input.Sorting, input.Filter);
            result.IsSuccess(ObjectMapper.Map <List<UserInfo>, List<UserInfoDto>>(data));
            return await Task.FromResult(result);
        }
    }
}
